<?php
namespace app\admin\controller;

use think\Db;
use think\facade\Session;

class User extends Base
{
	public function user()
	{
        if ($this->request->isPost() && $this->request->isAjax()) {
            // 查询
            $map = [];

            // 分页
            $page = $this->request->param('page/d', 0);
            $limit = $this->request->param('limit/d', 0);
            $count = Db::table('user')->where($map)->count();
            
            $data = Db::table('user')->where($map)->limit($limit * ($page - 1), $limit)->order('id desc')->select();

            if ($data) {
                $role = Db::table('role')->column('name', 'id');
                foreach ($data as $k => &$v) {
                    $v['role_name'] = array_key_exists($v['role_id'], $role) ? $role[$v['role_id']] : '';
                }
            }

            // 返回数据
            $res = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $data,
            ];

            return json($res);
        }

        $this->assign('role', Db::table('role')->column('name', 'id'));

		return $this->fetch();
	}

    public function userSave()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);
            $data = [
                'role_id'   =>  $this->request->param('role_id/d', 0),
                'name'      =>  trim($this->request->param('name/s', '')),
                'account'   =>  trim($this->request->param('account/s', '')),
                'password'  =>  trim($this->request->param('password/s', '')),
            ];

            // 校验重复
            $map = [];
            $map[] = ['id', '<>', $id];
            $map[] = ['account', '=', $data['account']];
            if (Db::table('user')->where($map)->count()) {
                $this->result('', 1, '账号重复');
            }

            // 添加编辑状态检测
            if ($id) {
                // 密码为空则不更改密码
                if ('' === $data['password']) {
                    unset($data['password']);
                } else {
                    $data['password'] = md5($data['password']);
                }

                if (!Db::table('user')->where('id', $id)->update($data)) {
                    $this->result('', 1, '编辑用户失败');
                }
            } else {
                // 初始化密码
                $data['password'] = '' === $data['password'] ? md5('123456') : md5($data['password']);

                if (!Db::table('user')->insert($data)) {
                    $this->result('', 1, '添加用户失败');
                }
            }

            $this->result('', 0, '保存成功');
        }
    }    

    public function userDel()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $id = $this->request->param('id/d', 0);

            if ($id == Session::get('user.id')) {
                $this->result('', 1, '禁止删除当前登录用户');
            }

            if (!Db::table('user')->delete($id)) {
                $this->result('', 1, '删除失败');
            }

            $this->result('', 0, '删除成功');
        }
    }
}
